package com.ovopark.organize.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ovopark.organize.server.mapper.FranchiseesApprovalAddressRegistrationMapper;
import com.ovopark.organize.server.model.entity.FranchiseesApprovalAddressRegistration;
import com.ovopark.organize.server.service.FranchiseesApprovalAddressRegistrationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @description: 选址点位和线索关系
 * @param:
 * @return:
 * @author wxb
 * @date: 2024/6/4 10:06
 */
@Service
@Slf4j
public class FranchiseesApprovalAddressRegistrationServiceImpl extends ServiceImpl<FranchiseesApprovalAddressRegistrationMapper, FranchiseesApprovalAddressRegistration> implements FranchiseesApprovalAddressRegistrationService {

    /**
     * @description: 删除
     * @param: [groupId, addressId, registrationId]
     * @return: void
     * @author wxb
     * @date: 2024/6/11 20:23
     */
    @Override
    public boolean deleteBy(Integer groupId, Integer addressId, Integer registrationId) {
        try {
            LambdaQueryWrapper<FranchiseesApprovalAddressRegistration> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(FranchiseesApprovalAddressRegistration::getGroupId, groupId);
            queryWrapper.eq(FranchiseesApprovalAddressRegistration::getAddressId, addressId);
            queryWrapper.eq(FranchiseesApprovalAddressRegistration::getRegistrationId, registrationId);
            baseMapper.delete(queryWrapper);
            return true;
        } catch (Exception e) {
            log.error("删除选址点位和线索关系失败", e);
            return false;
        }
    }

    /**
     * @description: 新增
     * @param: [groupId, addressId, registrationId]
     * @return: void
     * @author wxb
     * @date: 2024/6/11 20:23
     */
    @Override
    public boolean add(Integer groupId, Integer addressId, Integer registrationId, Integer userId) {
        try {
            FranchiseesApprovalAddressRegistration registration = new FranchiseesApprovalAddressRegistration();
            registration.setGroupId(groupId);
            registration.setAddressId(addressId);
            registration.setRegistrationId(registrationId);
            registration.setCreateTime(LocalDateTime.now());
            registration.setCreateId(userId);
            baseMapper.insert(registration);
            return true;
        } catch (Exception e) {
            log.error("新增选址点位和线索关系失败", e);
            return false;
        }
    }

    /**
     * @description: 修改
     * @param: [approvalAddressRegistration]
     * @return: boolean
     * @author wxb
     * @date: 2024/6/12 10:55
     */
    @Override
    public boolean update(FranchiseesApprovalAddressRegistration approvalAddressRegistration) {
        if (approvalAddressRegistration != null) {
            return this.updateById(approvalAddressRegistration);
        } else {
            return false;
        }
    }

    /**
     * @description: 获取
     * @param: [groupId, addressId, registrationId, userId]
     * @return: com.ovopark.organize.server.model.entity.FranchiseesApprovalAddressRegistration
     * @author wxb
     * @date: 2024/6/12 10:56
     */
    @Override
    public FranchiseesApprovalAddressRegistration getBy(Integer groupId, Integer addressId, Integer registrationId) {
        LambdaQueryWrapper<FranchiseesApprovalAddressRegistration> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FranchiseesApprovalAddressRegistration::getGroupId, groupId);
        queryWrapper.eq(FranchiseesApprovalAddressRegistration::getAddressId, addressId);
        queryWrapper.eq(FranchiseesApprovalAddressRegistration::getRegistrationId, registrationId);
        return this.baseMapper.selectOne(queryWrapper);
    }
}
